//
//  CYVersionUpdateView.m
//  cyydt
//
//  Created by chu on 2019/2/11.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYVersionUpdateView.h"

@interface CYVersionUpdateView ()
{
    CYVersionModel *_versionModel;
    UIButton *_btn1;
    UIButton *_btn2;
}
@property (nonatomic, strong) UIView *contrainerView;

@property (nonatomic, strong) UIView *headImageView;

@end

@implementation CYVersionUpdateView

- (instancetype)initWithVersionModel:(CYVersionModel *)versionModel{
    if (self = [super init]) {
        _versionModel = versionModel;
        [self initUI];
        [self initData];
    }
    return self;
}

- (void)initUI{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    self.userInteractionEnabled = YES;
    [self addSubview:self.contrainerView];
    [self addSubview:self.headImageView];
}

- (void)initData{
    NSArray *contents = _versionModel.version_content;
    CGFloat lastY = 105;
    CGFloat contentsHeight = 160;
    for (int i = 1; i <= contents.count; i++) {
        NSString *text = contents[i - 1];
        CGFloat height = [[Tool sharedInstance] heightForString:[NSString stringWithFormat:@"%d.%@", i, text] andWidth:self.contrainerView.frame.size.width - 60 fontSize:14];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, lastY, self.contrainerView.frame.size.width - 60, height)];
        label.text = [NSString stringWithFormat:@"%d.%@", i, text];
        label.textColor = RGBOF(0x666666);
        label.font = [UIFont systemFontOfSize:14];
        [self.contrainerView addSubview:label];
        lastY += height;
        lastY += 15;
        contentsHeight += 15;
        contentsHeight += height;
    }
    self.contrainerView.frame = CGRectMake(0, 0, kWindowW - 70, contentsHeight);
    self.contrainerView.center = self.center;
    
    self.headImageView.center = CGPointMake(kWindowW / 2, CGRectGetMinY(self.contrainerView.frame));
    
    [self layoutIfNeeded];
}

- (UIView *)contrainerView{
    if (!_contrainerView) {
        _contrainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW - 70, 140)];
        _contrainerView.backgroundColor = [UIColor whiteColor];
        _contrainerView.layer.cornerRadius = 10;
        _contrainerView.layer.masksToBounds = YES;
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _contrainerView.frame.size.width, 20)];
        label.text = @"发现新版本";
        label.textColor = RGBOF(0xF43636);
        label.font = [UIFont systemFontOfSize:18];
        label.textAlignment = NSTextAlignmentCenter;
        label.center = CGPointMake(_contrainerView.frame.size.width / 2, 75);
        [_contrainerView addSubview:label];
        
        UIView *bottomView = [[UIView alloc] init];
        bottomView.backgroundColor = RGBOF(0xefefef);
        [_contrainerView addSubview:bottomView];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contrainerView).with.mas_equalTo(0);
            make.bottom.equalTo(self.contrainerView).with.mas_equalTo(0);
            make.right.equalTo(self.contrainerView).with.mas_equalTo(0);
            make.height.mas_equalTo(50);
        }];
        NSArray *titles = @[@"暂不更新", @"立即更新"];
        CGFloat width = (_contrainerView.frame.size.width - 1) / 2;
        CGFloat height = 49;
        for (int i = 0; i < 2; i++) {
            UIButton *commentBtn = [[UIButton alloc] init];
            [commentBtn setBackgroundColor:[UIColor whiteColor]];
            [commentBtn setFrame:CGRectMake(width * i + 1 * i, 1, width, height)];
            [commentBtn setTitle:titles[i] forState:UIControlStateNormal];
            [commentBtn addTarget:self action:@selector(btnsClick:) forControlEvents:UIControlEventTouchUpInside];
            commentBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            [bottomView addSubview:commentBtn];
            commentBtn.tag = i;
            if (i == 0) {
                [commentBtn setTitleColor:RGBOF(0x666666) forState:UIControlStateNormal];
                _btn1 = commentBtn;
            }
            if (i == 1) {
                [commentBtn setTitleColor:RGBOF(0xF43636) forState:UIControlStateNormal];
                _btn2 = commentBtn;
            }
        }
    }
    return _contrainerView;
}

- (void)btnsClick:(UIButton *)sender{
    if (sender.tag == 0) {
        [self dismiss];
    }else{
        //版本更新
        NSURL *url = [NSURL URLWithString:_versionModel.version_url];
        if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                if (@available(iOS 10.0, *)) {
                    [[UIApplication sharedApplication] openURL:url options:@{}
                                             completionHandler:^(BOOL success) {
                                                 if (success) {
                                                     NSLog(@"可以打开");
                                                 }else{
                                                     NSLog(@"不可以打开");
                                                 }
                                             }];
                } else {
                    BOOL success = [[UIApplication sharedApplication] openURL:url];
                    if (success) {
                        
                    }else{
                        
                    }
                }
            } else {
                BOOL success = [[UIApplication sharedApplication] openURL:url];
                if (success) {
                    
                }else{
                    
                }
            }
            
        } else{
            bool can = [[UIApplication sharedApplication] canOpenURL:url];
            if(can){
                [[UIApplication sharedApplication] openURL:url];
            }
        }
    }
}

- (UIView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 187, 125)];
        UIImage *image = [UIImage imageNamed:@"gengxin"];
        _headImageView.layer.contents = (id)image.CGImage;
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _headImageView.frame.size.width, 20)];
        label.text = _versionModel.version_name;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.center = CGPointMake(_headImageView.frame.size.width / 2 - 20, _headImageView.frame.size.height / 2);
        [_headImageView addSubview:label];
    }
    return _headImageView;
}

- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.35 animations:^{
        
    }];
    
    
}

- (void)dismiss {
    [self removeFromSuperview];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    // 1.这是当前点击的视图，如果没有找到合适的响应操作的视图，则直接返回这个
    UIView *view = [super hitTest:point withEvent:event];
    
    // 2.将父视图坐标转成我想要响应事件的视图的坐标
    CGPoint buttonPoint = [self convertPoint:point toView:self.contrainerView];
    
    // 3.判断该坐标是否在视图内部，如果是，则返回该视图
    if ([self.contrainerView pointInside:buttonPoint withEvent:event]) {
        CGPoint buttonPoint1 = [self convertPoint:point toView:_btn1];
        CGPoint buttonPoint2 = [self convertPoint:point toView:_btn2];
        if ([_btn1 pointInside:buttonPoint1 withEvent:event]) {
            [self btnsClick:_btn1];
            return _btn1;
        }else if ([_btn2 pointInside:buttonPoint2 withEvent:event]){
            [self btnsClick:_btn2];
            return _btn2;
        }
        return self.contrainerView;
    }else{
        [self dismiss];
        return view;
    }
}

@end
